package Demo;

import Demo.ClassPath.ClassPath;

import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        Command command = Command.parse(args);
        if(!command.ok || command.helpFlag){
            System.out.println("用法: java [-options] class [args...]\n" +
                    "           (执行类)\n" +
                    "   或  java [-options] -jar jarfile [args...]\n" +
                    "           (执行 jar 文件)");
        }else if (command.versionFlag) {
            System.out.println("openjdk version \"1.8.0_242\"\n" +
                    "OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_242-b08)\n" +
                    "OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.242-b08, mixed mode)");
        }else{
            runJvm(command);
        }
    }
    static void runJvm(Command command){
        ClassPath cp = new ClassPath(command.jre,command.classpath);
        System.out.printf("classpath:%s class:%s args:%s\n", command.classpath, command.getMainClass(), command.getAppArgs());
        String ClassName = command.getMainClass().replace(".","/");
        try{
            byte[] classData = cp.readClass(ClassName);
            System.out.println("ClassData：");
            for (byte b : classData) {
                //16进制输出
                System.out.print(String.format("%02x", b & 0xff) + " ");
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
